package playing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Begin extends JFrame {
    public Begin() {
        // 设置窗口标题和大小
        setTitle("欢迎来到pp农场");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建主面板
        JPanel mainPanel = new JPanel() {
            // 绘制背景图片
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                ImageIcon bgImage = new ImageIcon("lib/backg.png"); // 替换为你的图片路径
                g.drawImage(bgImage.getImage(), 0, 0, getWidth(), getHeight(), null);
            }
        };
        mainPanel.setLayout(new GridBagLayout());

        // 添加欢迎标签
        JLabel welcomeLabel = new JLabel("欢迎来到pp农场", SwingConstants.CENTER);
        welcomeLabel.setForeground(Color.WHITE); // 设置标签文本颜色为白色，以便在背景图片上更好地显示

        // 添加开始游戏按钮
        JButton loginButton = new JButton("开始游戏");
        loginButton.setPreferredSize(new Dimension(100, 30)); // 设置按钮的宽度为100，高度为30
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();  // 打开登录窗口
                dispose();  // 关闭当前Begin窗口
            }
        });

        // 使用GridBagLayout添加组件
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(10, 10, 10, 10); // 设置外边距
        gbc.anchor = GridBagConstraints.NORTH;
        mainPanel.add(welcomeLabel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.CENTER;
        mainPanel.add(loginButton, gbc);

        add(mainPanel);
        setVisible(true);
    }

    public static void main(String[] args) {
        // 启动Begin窗口
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Begin();
            }
        });
    }
}
